package com.zhiyou100.cudr;

import com.zhiyou100.dao.StudentDao;
import com.zhiyou100.entityclass.Student;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @packageName: ${NAME}
 * @className: ${PACKAGE_NAME}
 * @Description: TODO 更改一个
 * @author: yang
 * @date: 7/20/20
 */
@WebServlet(name = "ChangeOne", urlPatterns = "/changeOne")
public class ChangeOne extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        // 设定编码集
        response.setContentType("text/html;charset=UTF-8");
        // 设定类型



        String sid = request.getParameter("sid");
        String sname = request.getParameter("sname");
        String sex = request.getParameter("sex");
        String sage = request.getParameter("sage");
        String scid = request.getParameter("scid");

        StudentDao studentDao = new StudentDao();

        boolean reasonableJustification = studentDao.reasonableJustification(sid, sname, sex, sage, scid);
        if (reasonableJustification){
            // 合理，创建对象，改变
            Student m = new Student(Long.parseLong(sid)
                    , sname, sex.equals("m") ? "男" : "女",
                    Long.parseLong(sage),
                    Long.parseLong(scid));
            System.out.println(m);


            boolean b = studentDao.changeOne(m);
            request.setAttribute("changeFlag", b);
            request.getRequestDispatcher("/jsp/changeOne.jsp").forward(request,response);
        }else {
            // 不合理，不创建对象，不改变
            request.setAttribute("changeFlag",false);
            request.getRequestDispatcher("/jsp/changeOne.jsp").forward(request,response);
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
